<?php
/*
 * Created on 22.12.2005
 *
 */

m2import('phpinchains/util/ArrayWalker.php');

class h_inputHandler
{
    
    var $tagName = 'input'; 
    var $containerTag = false; 

    function startElement (phpinchains_parser_psp_PSPParser & $sender, $attrs)
    {
        $form = $sender->topParent();
        if (!isset($form['type']) || $form['type'] != 'form')
        {
            die('h:'.$this->tagname.' must not be used within other tag than h:form (except flow control tags)!');
        }
        if (!empty($attrs['validate']) && (empty($form['name']) || empty($attrs['name'])))
        {
            die("If h:'.$this->tagname.' tag has attribute validate than it also needs attribute name and the surrounding form tag must have attribute name, too!");
        }
        if (!empty($form['name']) && empty($attrs['validate']))
        {
            $sender->addFormField(substr($form['name'], 1, -1), substr($attrs['name'], 1, -1));
        }
        if (!empty($attrs['type']) && $attrs['type'] == "'file'")
        {
            $sender->addFileField(substr($form['name'], 1, -1), substr($attrs['name'], 1, -1));
        }
        // tag code
        $code = "\$out->write('<".$this->tagName;
        foreach ($attrs as $n => $v)
        {
            if ($n != 'pspc' && $n != 'validate' && $n != 'validationParam' 
                && $n != 'validationErrorClass' && $n != 'validationErrorStyle'
                && $n != 'class' && $n != 'style' && $n != 'checked')
            {
                if ($n=='value') {
                	if ($attrs['type'] != "'file'") 
                        $code .= " $n=\"'.htmlentities($v, ENT_COMPAT, MODEL2_ENCODING).'\"";
                } else
                    $code .= " $n=\"'.$v.'\"";
            }
        }
        $code .= "');\n";
        if ($attrs['checked'])
            $code .= 'if ('.$attrs['checked'].') $out->write(" checked=\"checked\"");'."\n";
        $code .= "\$out->write(phpinchains_tagsupport_HtmlInput::getValidationErrorCode(\$actionform, ".(isset($attrs['validate'])?$attrs['validate']:"''").", ".
            (isset($form['name'])?$form['name']:"''").", ".(isset($attrs['name'])?$attrs['name']:"''").", ".(isset($attrs['class'])?$attrs['class']:"''").", ".(isset($attrs['style'])?$attrs['style']:"''").", ".
            (isset($attrs['validationErrorClass'])?$attrs['validationErrorClass']:"''").", ".(isset($attrs['validationErrorStyle'])?$attrs['validationErrorStyle']:"''")."));\n";
        if ($this->containerTag)
            $code .= "\$out->write('>');\n";
        else
            $code .= "\$out->write('/>');\n";
        $sender->appendCode($code);
        // validation code
        $code = '';
        if ($attrs['validate'])
        {
            //$code .= "\n        \$this->validateField(\$env, \$actionform, ".$attrs['name'].", ".(isset($attrs['validationParam'])?$attrs['validationParam']:"''").', '.$attrs['validate'].");\n";
            //$sender->appendValidationCode($code);
            if (strpos($attrs['validate'],',') == -1) {
                $sender->appendValidationField(substr($form['name'], 1, -1), substr($attrs['name'], 1, -1), $attrs['validate'], $attrs['validationParam']);
            } else {
                $validate = ArrayWalker::trim(split(',', substr($attrs['validate'], 1, -1)));
                foreach ($validate as $v) {
                    $sender->appendValidationField(substr($form['name'], 1, -1), substr($attrs['name'], 1, -1), "'".$v."'", $attrs['validationParam']);
                }
            }
        }
    }

    function endElement (& $sender)
    {
    }

}

?>